<?php
/**
 * Image
 * - Class process images such as:
 *   +) Resize
 *   +) Copy image from $_FILES
 * - Copyright&copy; Runsystem
 * @author Do Truong Giang
 * @since 1.0 - Jul 19, 2008
 * $Id: Image.class.php,v 1.1
 */
include_once(DOCUMENT_ROOT . "/lib/core/UploadFile.class.php");

class Image extends UploadFile{
	/**
	 * getDimension
	 * - Return the dimension of image
	 * @param String $image The absolute path to image file
	 * @return Array Element 0 is width, element 1 is height
	 */
	public static function getDimension($image) {
		list($width, $height) = getimagesize($image);
		return array($width, $height);
	}
	/**
	 * getType
	 * - Return the type of image in string format
	 * @param String $image The absolute path to image file
	 * @return String The name of type. See the list of array inside this function to know the return string
	 */
	public static function getType($image) {
		list($w, $h, $type) = getimagesize($image);
		$typeArr = array(1 => "gif", 2 => "jpg", 3 => "png", 4 => "swf",
						5 => "psd", 6 => "bmp", 7 => "tiff1", 8 => "tiff2",
						9 => "jpc", 10 => "jp2", 11 => "jpx", 12 => "jb2",
						13 => "swc", 14 => "iff", 15 => "wbmp", 16 => "xbm");
		return $typeArr[$type];
	}
	/**
	 * resize
	 * - Resize an image using GD2
	 * - Only resize when width or height of image exceed max width or height.
	 * - At this moment, only support GIF, JPEG, PNG
	 * @param String $image The file path of image
	 * @param Integer $width Max width of resized image
	 * @param Integer $height Max height of resized image
	 * @return Boolean True if successful, otherwise false
	 */
	public static function resize($image, $width, $height) {
		// Calculate the true width and height of new image
		$trueWidth = 0;
		$trueHeight = 0;
		list($imgW, $imgH) = self::getDimension($image);
		if ($imgW > $width || $imgH > $height) {
			$ratioW = (float) $width / $imgW;
			$ratioH = (float) $height / $imgH;
			$ratio = min($ratioW, $ratioH);
			$trueWidth = round($imgW * $ratio);
			$trueHeight = round($imgH * $ratio);
		}
		else {
			return true;
		}
		// Get the type of image
		$type = self::getType($image);
		// Create the resource to load image
		$imgResource = null;
		switch ($type) {
			case "gif":
				$imgResource = imagecreatefromgif($image);
				break;
			case "jpg":
				$imgResource = imagecreatefromjpeg($image);
				break;
			case "png":
				$imgResource = imagecreatefrompng($image);
				break;
			default:
				return false;
		}
		// Create destination resource
		$destResource = imagecreatetruecolor($trueWidth, $trueHeight);
		// Copy the source resource to destination resource
		imagecopyresized($destResource, $imgResource, 0, 0, 0, 0, $trueWidth, $trueHeight, $imgW, $imgH);
		switch ($type) {
			case "gif":
				imagegif($destResource, $image);
				break;
			case "jpg":
				imagejpeg($destResource, $image);
				break;
			case "png":
				imagepng($destResource, $image);
				break;
		}
		return true;
	}
	/**
	 * copy
	 * - Copy an uploaded image to a new folder
	 * - Resize the image 
	 * @param String $fileName The name of &lt;input type='file'&gt;
	 * @param String $newName The absolute path of new image file
	 * @param Integer $width Max width of image
	 * @param Integer $height Max height of image
	 */
	public static function copy($fileName, $newName, $width, $height) {
		$success = parent::copy($fileName, $newName);
		if ($success === true) {
			$success = self::resize($newName, $width, $height);
		}
		return $success;
	}
}
?>